package Test;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.table.*;

public class PersonManagementSystem extends JFrame {
    private DefaultTableModel personModel = new DefaultTableModel() {
        @Override
        public boolean isCellEditable(int row, int column) {
            return false;
        }
    };
    private JTable table = new JTable(personModel);
    private JPopupMenu popupMenu = new JPopupMenu();
    private JMenuItem openMenuItem = new JMenuItem("打开");
    private JMenuItem saveMenuItem = new JMenuItem("保存草稿");
    private JMenuItem selectFileMenuItem = new JMenuItem("选择文件");
    private JButton deleteSelectedItemButton = new JButton("删除选中项");
    private JButton deleteSelectedItemsButton = new JButton("删除选中多项");
    private JButton addPersonButton = new JButton("添加");
    private JButton updatePersonButton = new JButton("修改");
    private JButton savePersonButton = new JButton("保存");
    private JComboBox<String> provinceComboBox = new JComboBox<>();
    private JComboBox<String> cityComboBox = new JComboBox<>();
    private JCheckBox multiSelectCheckBox = new JCheckBox("多选");
    private JTextField NameField = new JTextField();
    private JSpinner birthDateSpinner = new JSpinner();

    private ArrayList<Person> personList = new ArrayList<>();
    private int selectedRowIndex = -1;

    public PersonManagementSystem() {
        // 初始化界面
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.anchor = GridBagConstraints.WEST;
        c.gridx++;
        panel.add(new JLabel("姓名："), c);
        c.gridx++;
        panel.add(NameField, c);
        c.gridx++;
        panel.add(new JLabel("出生日期："), c);
        c.gridx++;
        panel.add(birthDateSpinner, c);
        c.gridx++;
        panel.add(new JLabel("省份："), c);
        c.gridx++;
        panel.add(provinceComboBox, c);
        c.gridx++;
        panel.add(new JLabel("城市："), c);
        c.gridx++;
        panel.add(cityComboBox, c);

        c.gridx = 0;
        c.gridy = 1;
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 10.0;
        c.weighty = 10.0;
        c.gridwidth = 10;
        panel.add(new JScrollPane(table), c);

        c.gridx = 0;
        c.gridy = 2;
        c.fill = GridBagConstraints.NONE;
        c.weightx = 10.0;
        c.weighty = 10.0;
        c.gridwidth = 2;
        panel.add(multiSelectCheckBox, c);
        c.gridx = 2;
        panel.add(deleteSelectedItemButton, c);
        c.gridx = 4;
        panel.add(deleteSelectedItemsButton, c);
        c.gridx = 6;
        panel.add(addPersonButton, c);
        c.gridx = 8;
        panel.add(updatePersonButton, c);
        c.gridx = 9;
        panel.add(savePersonButton, c);

        add(panel);

        // 初始化表格和菜单
        personModel.addColumn("姓名");
        personModel.addColumn("出生日期");
        personModel.addColumn("省份");
        personModel.addColumn("城市");
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.setComponentPopupMenu(popupMenu);
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON1) {
                    selectedRowIndex = table.rowAtPoint(e.getPoint());
                    if (selectedRowIndex >= 0) {
                        Person person = personList.get(selectedRowIndex);
                        NameField.setText(person.getName());
                        birthDateSpinner.setValue(person.getBirthDate());
                        provinceComboBox.setSelectedItem(person.getProvince());
                        cityComboBox.setSelectedItem(person.getCity());
                    }
                } else if (e.getButton() == MouseEvent.BUTTON3) {
                    int rowIndex = table.rowAtPoint(e.getPoint());
                    if (rowIndex >= 0) {
                        table.setRowSelectionInterval(rowIndex, rowIndex);
                        popupMenu.show(table, e.getX(), e.getY());
                    }
                }
            }
        });

        popupMenu.add(openMenuItem);
        popupMenu.add(saveMenuItem);
        popupMenu.addSeparator();
        popupMenu.add(selectFileMenuItem);

        // 初始化省份和城市组合框
        provinceComboBox.setEditable(true);
        cityComboBox.setEditable(true);

        // 添加事件监听器
        // 添加按钮事件监听器
        addPersonButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String Name = NameField.getText().trim();
                String birthDate = birthDateSpinner.getValue().toString();
                String province = provinceComboBox.getSelectedItem().toString();
                String city = cityComboBox.getSelectedItem().toString();
                if (!Name.isEmpty()) {
                    Person person = new Person(Name, province, city, birthDate);
                    personList.add(person);
                    personModel.addRow(new Object[]{person.getName(),
                            person.getBirthDate(),
                            person.getProvince(),
                            person.getCity()});
                } else {
                    JOptionPane.showMessageDialog(PersonManagementSystem.this,
                            "请输入姓名", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        // 修改按钮事件监听器
        updatePersonButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (selectedRowIndex >= 0) {
                    String Name = NameField.getText().trim();
                    String birthDate = birthDateSpinner.getValue().toString();
                    String province = provinceComboBox.getSelectedItem().toString();
                    String city = cityComboBox.getSelectedItem().toString();
                    if (!Name.isEmpty()) {
                        Person person = personList.get(selectedRowIndex);
                        person.setName(Name);
                        person.setBirthDate(birthDate);
                        person.setProvince(province);
                        person.setCity(city);
                        personModel.setValueAt(Name, selectedRowIndex, 0);
                        personModel.setValueAt(birthDate, selectedRowIndex, 1);
                        personModel.setValueAt(province, selectedRowIndex, 2);
                        personModel.setValueAt(city, selectedRowIndex, 3);
                    } else {
                        JOptionPane.showMessageDialog(PersonManagementSystem.this,
                                "请输入姓名", "错误", JOptionPane.ERROR_MESSAGE);
                    }
                } else {
                    JOptionPane.showMessageDialog(PersonManagementSystem.this,
                            "请先选择要修改的项", "提示", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });

        // 保存按钮事件监听器
        savePersonButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 保存数据到文件或数据库
                JOptionPane.showMessageDialog(PersonManagementSystem.this,
                        "保存成功", "提示", JOptionPane.INFORMATION_MESSAGE);
            }
        });

        // 删除选中项按钮事件监听器
        deleteSelectedItemButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int rowIndex = table.getSelectedRow();
                if (rowIndex >= 0) {
                    personList.remove(rowIndex);
                    personModel.removeRow(rowIndex);
                }
            }
        });

        // 删除选中多项按钮事件监听器
        deleteSelectedItemsButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int[] indices = table.getSelectedRows();
                for (int i = indices.length - 1; i >= 0; i--) {
                    personList.remove(indices[i]);
                    personModel.removeRow(indices[i]);
                }
            }
        });

        // 初始化窗体
        setTitle("Person Management System");
        setSize(800, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    // 内部类，表示一个人的基本信息
    private class Person {
        private String Name;
        private String province;
        private String city;
        private String birthDate;

        public Person(String Name, String province,
                      String city, String birthDate) {
            this.Name = Name;
            this.province = province;
            this.city = city;
            this.birthDate = birthDate;
        }

        public String getName() {
            return Name;
        }

        public void setName(String Name) {
            this.Name = Name;
        }

        public String getProvince() {
            return province;
        }

        public void setProvince(String province) {
            this.province = province;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getBirthDate() {
            return birthDate;
        }

        public void setBirthDate(String birthDate) {
            this.birthDate = birthDate;
        }
    }

    public static void main(String[] args) {
        new PersonManagementSystem().setVisible(true);
    }
}